package com.yeming.flink.practice.watermark

import java.text.SimpleDateFormat

import com.yeming.flink.practice.source.StationLog
import org.apache.flink.api.common.functions.ReduceFunction
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.scala.function.WindowFunction
import org.apache.flink.streaming.api.watermark.Watermark
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector

/**
 * 每隔五秒计算一下最近十秒内，每个基站中通话时长最长的一次通话发生的时间，
 * 还有主叫号码，被叫号码，通话时长，并且还告诉我们当前发生的时间范围
 * watermark允许最大的延迟是3秒。
 */
object TestWaterMark2 {

  def main(args: Array[String]): Unit = {

    val streamEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    streamEnv.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

    streamEnv.setParallelism(1)
    streamEnv.getConfig.setAutoWatermarkInterval(100L) //周期性地引入watermark设置，默认的时间间隔就是100毫秒
    val stream: DataStream[StationLog] = streamEnv.socketTextStream("localhost", 9999).map(line => {
      val arr: Array[String] = line.split(",")
      new StationLog(arr(0).trim, arr(1).trim, arr(2).trim, arr(3).trim, arr(4).trim.toLong, arr(5).trim.toLong)
    })
    //引入无序的数据，并且通过观察延迟的时间是3秒，采用周期性的watermark引入
      //代码写法又两种
      //第一种
//      .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[StationLog](Time.seconds(3)) {
//        //抽取时间
//        override def extractTimestamp(element: StationLog): Long = {
//          element.callTime
//        }
    //第二种写法
      .assignTimestampsAndWatermarks(new AssignerWithPeriodicWatermarks[StationLog] {
        var maxEventTime:Long = _
        override def getCurrentWatermark: Watermark = {
          new Watermark(maxEventTime - 3000L)
        }

        override def extractTimestamp(element: StationLog, previousElementTimestamp: Long): Long = {
          maxEventTime = maxEventTime.max(element.callTime)
          element.callTime
        }
      })

    stream.filter(_.callType.equals("success"))
      .keyBy(_.sid)
      .timeWindow(Time.seconds(10),Time.seconds(5))
      .reduce(new MyReduceFunction,new ReturnMaxTimeWindowFunction)
      .print()

    streamEnv.execute("TestWatermark")
  }

  class MyReduceFunction extends ReduceFunction[StationLog] {//增量聚合
    override def reduce(value1: StationLog, value2: StationLog): StationLog = {
      if (value1.duration < value2.duration) value2 else value1
    }
  }

  class ReturnMaxTimeWindowFunction extends WindowFunction[StationLog, String, String,TimeWindow] {
    override def apply(key: String, window: TimeWindow, input: Iterable[StationLog], out: Collector[String]): Unit = {
      val format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
      val log: StationLog = input.iterator.next()
      val str: String = String.format("窗口时间范围：%s-%s, 主叫号码：%s, 被叫号码：%s, 通话时长：%d秒.", format.format(window.getStart), format.format(window.getEnd),
        log.callOut, log.callIn, Long.box(log.duration))
      out.collect(str)
    }
  }
}
